using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Microsoft.WowAddonStudio.FrameXml.Components.Events.Editors;
using Microsoft.WowAddonStudio.FrameXml.Serialization;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Events
{
    /// <summary>
    /// Represents a FrameXML event handler definition.
    /// </summary>
    [Serializable]
    [TypeConverter(typeof(FrameXmlEventTypeConverter))]
    [Editor(typeof(EventSelectorEditor), typeof(UITypeEditor))]
    public class FrameXmlEventHandler : ICloneable, ISerializable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FrameXmlEventHandler"/> class.
        /// </summary>
        /// <param name="functionName">Name of the function.</param>
        /// <param name="eventName">Name of the event.</param>
        public FrameXmlEventHandler(string functionName, EventName eventName) 
        {
            ScriptBlock = functionName;
            EventName = eventName;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FrameXmlEventHandler"/> class.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <param name="context">The context.</param>
        protected FrameXmlEventHandler(SerializationInfo info, StreamingContext context) 
        {
            if (info != null)
            {
                ScriptBlock = info.GetString("FunctionName");
                EventName = (EventName) Enum.Parse(typeof (EventName), info.GetString("EventName"));
            }
        }

        /// <summary>
        /// Gets or sets the name of the frame XML event.
        /// </summary>
        /// <value>The name of the frame XML event.</value>
        public EventName EventName { get; set; }

        /// <summary>
        /// Gets or sets the name of the function.
        /// </summary>
        /// <value>The name of the function.</value>
        public string ScriptBlock { get; set; }

        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString()
        {
            return ScriptBlock;
        }

        #region ICloneable Members

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            return new FrameXmlEventHandler(ScriptBlock, EventName);
        }

        #endregion

        #region ISerializable Members

        /// <summary>
        /// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data needed to serialize the target object.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data.</param>
        /// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"/>) for this serialization.</param>
        /// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if(info != null)
            {
                info.AddValue("FunctionName", ScriptBlock);
                info.AddValue("EventName", EventName);
            }
        }

        #endregion
    }
}